package com.hcy.reasult;

/**
 * @Description:
 * @Author: LiJun
 * @Date: 2025/7/23 17:03
 */

public class Result<T> {
    private int code;
    private String msg;
    private T data;
    private Pagination pagination;
    // 成功状态码常量
    public static final int SUCCESS_CODE = 200;
    // 错误状态码常量
    public static final int ERROR_CODE = 500;
    private Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
    // 分页信息静态内部类
    public static class Pagination {
        private long total;      // 总记录数
        private int page;        // 当前页码
        private int pageSize;    // 每页数量
        private int totalPages;  // 总页数
        public Pagination(long total, int page, int pageSize) {
            this.total = total;
            this.page = page;
            this.pageSize = pageSize;
            this.totalPages = (int) Math.ceil((double) total / pageSize);
        }
        // Getters
        public long getTotal() { return total; }
        public int getPage() { return page; }
        public int getPageSize() { return pageSize; }
        public int getTotalPages() { return totalPages; }
    }
    // ===== 成功结果快捷方法 =====
    public static <T> Result<T> success() {
        return new Result<>(SUCCESS_CODE, "操作成功", null);
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(SUCCESS_CODE, "操作成功", data);
    }

    public static <T> Result<T> success(String msg, T data) {
        return new Result<>(SUCCESS_CODE, msg, data);
    }

    public static <T> Result<T> success(T data, Pagination pagination) {
        Result<T> result = new Result<>(SUCCESS_CODE, "操作成功", data);
        result.setPagination(pagination);
        return result;
    }

    // ===== 错误结果快捷方法 =====
    public static <T> Result<T> error() {
        return new Result<>(ERROR_CODE, "操作失败", null);
    }

    public static <T> Result<T> error(String msg) {
        return new Result<>(ERROR_CODE, msg, null);
    }

    public static <T> Result<T> error(int code, String msg) {
        return new Result<>(code, msg, null);
    }

    // ===== 链式调用方法 =====
    public Result<T> code(int code) {
        this.code = code;
        return this;
    }

    public Result<T> msg(String msg) {
        this.msg = msg;
        return this;
    }

    public Result<T> data(T data) {
        this.data = data;
        return this;
    }

    public Result<T> pagination(long total, int page, int pageSize) {
        this.pagination = new Pagination(total, page, pageSize);
        return this;
    }

    // ===== 状态检查方法 =====
    public boolean isSuccess() {
        return code == SUCCESS_CODE;
    }

    // ===== Getters & Setters =====
    public int getCode() { return code; }
    public void setCode(int code) { this.code = code; }
    public String getMsg() { return msg; }
    public void setMsg(String msg) { this.msg = msg; }
    public T getData() { return data; }
    public void setData(T data) { this.data = data; }
    public Pagination getPagination() { return pagination; }
    public void setPagination(Pagination pagination) {
        this.pagination = pagination;
    }
}
